package com.unc.allergyex.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.unc.allergyex.R;
import com.unc.allergyex.objects.db.Component;

/**This is the ArrayAdapter for the dish-specific screens.  
 * It is used by the DishBreakdown activity to display the components of a dish.  
 * It uses the expanded_item.xml layout file.  
 * It uses ObjManager to get information about the list of given components.
 * @author Stephanie
 *
 */
public class ComponentAdapter extends ArrayAdapter<Component>{

	private ArrayList<Component> components;	

	/**Constructor for the adapter.  
	 * @param context the context into which the notifications should be made
	 * @param textViewResourceId default android, not used for anything
	 * @param items the list of components to display
	 */
	public ComponentAdapter(Context context, int textViewResourceId,
			ArrayList<Component> items) {
		super(context, textViewResourceId, items);
		components = items;
	}
	
	/**The method for inflating the view of a component.  
	 * Inflates the view, sets the background color, sets the text, and sets the appropriate image. 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//inflate the view into the context
		View v;
		LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		v = vi.inflate(R.layout.expanded_item, null);
		
		//set the background color dynamically, so that we can share the expanded_item.xml file between this adapter and the dishadapter
		LinearLayout background = (LinearLayout) v.findViewById(R.id.expanded);
		background.setBackgroundColor(0xFFFFFFFF);


		Component c = components.get(position);
		if (c != null) {
			
			//set the name of the component
			TextView componentName = (TextView) v.findViewById(R.id.expanded_name);			
			if(componentName != null){
				componentName.setText(c.getName());
			}
			
			//set the image to a check if the user is not allergic and to an ex if they are allergic
			//we need to draw it either way so that the list items are the same size - for some reason it shrinks it a little bit when you draw the image dynamically
			ImageView edible = (ImageView) v.findViewById(R.id.edible);
			if(c.getAllergic() == false && edible != null){
				edible.setImageResource(R.drawable.check);
			}
			else {
				edible.setImageResource(R.drawable.ex);
			}
		}
		return v;
	}
}